package com.marketlive.system.config.bundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.marketlive.system.config.IConfigurationBranch;

import java.util.*;

import com.marketlive.system.config.ConfigurationBranch;

/**
 * Exposes the <code>ConfigurationTree</code> as a <code>ResourceBundle</code>.  Returns a <code>List</code> of keys for all
 * leaves on the tree.
 */
public class ConfigurationResourceBundle extends ResourceBundle {

    private List keys;
    private ConfigurationBranch startingBranch;
    /**
     * Logger
     */
    private static Log log = LogFactory.getLog(ConfigurationResourceBundle.class);

    /**
     * Specifies the starting <code>IConfigurationBranch</code> to use.
     * @param startingBranch
     */
    public ConfigurationResourceBundle(IConfigurationBranch startingBranch) {
        this.startingBranch = (ConfigurationBranch) startingBranch;
        loadKeys();
    }

    /**
     * Caches the <code>List</code> of <code>Key</code>s from all leaves on the tree. 
     *
     */
    public void loadKeys() {
        keys = new ArrayList();
        startingBranch.getLeafNames(keys,null);
    }

    /**
     * Returns an enumeration of the keys.
     * @return an enumeration of the keys
     */
    public Enumeration getKeys() {
        return new EnumerationWrapper(keys.iterator());
    }

    /**
     * Gets an object for the given key from this resource bundle.
     * Returns null if this resource bundle does not contain an
     * object for the given key.
     *
     * @param key for the desired object
     * @return the object for the given key, or null
     * @throws NullPointerException if <code>key</code> is <code>null</code>
     */
    protected Object handleGetObject(String key) {
        return startingBranch.getAsString(key);
    }

    /**
     * Inner class that wraps the Enumeration implementation
     */
    class EnumerationWrapper implements Enumeration {

        final Iterator iterator;

        public EnumerationWrapper(Iterator iterator) {
            this.iterator = iterator;
        }

        /**
         * Tests if this enumeration contains more elements.
         *
         * @return <code>true</code> if and only if this enumeration object
         *         contains at least one more element to provide;
         *         <code>false</code> otherwise.
         */
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        /**
         * Returns the next element of this enumeration if this enumeration
         * object has at least one more element to provide.
         *
         * @return the next element of this enumeration.
         * @throws java.util.NoSuchElementException
         *          if no more elements exist.
         */
        public Object nextElement() {
            return iterator.next();
        }
    }
}
